// pages/wx_avatarUrl/wx_avatarUrl.js
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const $get_api = require('../../common/api').HTTP;
const setInfo = require('../../const').SET_INFO;
const until = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: defaultAvatarUrl,
        defaultAvatarUrl:defaultAvatarUrl,
        theme: wx.getSystemInfoSync().theme,
        nickname:'',
        until:until,
        share_route_obj:{}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options,"optionsoptionsoptions");
        let that = this
        that.data.share_route_obj = options
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
      

    },
    onChooseAvatar(e) {
        let that = this;
        // console.log(e,"e");
        const { avatarUrl } = e.detail;
        this.setData({
          avatarUrl,
        })
        wx.uploadFile({
            filePath: avatarUrl,
            name: 'file',
            url: 'https://nqxxcx.whyimingkeji.com/api/ajaxUpload?token='+wx.getStorageSync('token'),//服务器端接收图片的路径
            success:function(res){
              console.log(res);//发送成功回调
              let data = res.data;
              data = (JSON.parse(data))
              if(data.code != 0){
                app.showError(data.message)
                return false;
             }
             data = data.data;
             that.data.avatarUrl = data.url;
             
            },
            fail:function(res){
              console.log(res);//发送失败回调，可以在这里了解失败原因
            }
        })
    },
    updateUserInfo:function(nickname,head_img) {
      let that = this
        $get_api.updateUserInfo({ 
            nickname:nickname,
            head_img:head_img,
        }).then(r => { 
            let data = r.data.data;
            if(r.data.code == 0){
                app.showSuccess(r.data.message);
                wx.showLoading('请求中')
                setTimeout(() => {

                  var pages = getCurrentPages();
                  if(setInfo.tapbarList.indexOf(pages[pages.length-2].route) > -2){
                      wx.reLaunch({
                          url: '/' + pages[pages.length-2].route,
                      });
                  }else{

                    if(Object.keys(that.data.share_route_obj).length === 0){
                      wx.navigateTo({
                        url: '/' + pages[pages.length-2].route,
                      });
                    }else{
                      wx.navigateTo({
                        url: '/' + pages[pages.length-2].route + "?" + until.urlEncode(that.data.share_route_obj),
                    });
                  }
                      
                  }

                    // wx.switchTab({
                    //   url: '../personData/personData',
                    // })
                }, 1500);
            }
        })
    },
    getInputName:function(e){
        let that = this;
        that.data.nickname = e.detail.value;
        this.setData({
           nickname:e.detail.value,
        })
    },
    gotosave:function(){
        let that = this;
        let avatarUrl = that.data.avatarUrl;
        if(avatarUrl === that.data.defaultAvatarUrl){
            app.showError('请上传你的头像！')
            return ;
        }else if(that.data.nickname == ''){
            app.showError('请填写你的用户昵称！')
            return ;
        }else{
            let userInfo = JSON.parse(wx.getStorageSync('userInfo')) 
            userInfo.head_img = that.data.avatarUrl;
            userInfo.nickname = that.data.nickname;
            wx.setStorageSync('userInfo',JSON.stringify(userInfo))
            that.updateUserInfo(that.data.nickname,that.data.avatarUrl);
        }
        // console.log(that.data.nickname);
       
    }
})